package com.jnu.student;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.student.fragment.GameFragment;
import com.jnu.student.fragment.MapFragment;
import com.jnu.student.fragment.bookItemFragment;
import com.jnu.student.fragment.webViewFragment;

public class MainActivity extends AppCompatActivity {

    public static final int book = 0;
    public static final int news = 1;
    public static final int map = 2;
    public static final int game = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPagerFragments=findViewById(R.id.viewpager2_content);
        viewPagerFragments.setAdapter(new MyFragmentAdapter(this));

        TabLayout tabLayoutHeader= findViewById(R.id.tabLayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader, viewPagerFragments, (tab, position) -> {
            switch (position){
                case book:
                    tab.setText(getString(R.string.tab_book));
                    break;
                case news:
                    tab.setText(getString(R.string.tab_news));
                    break;
                case map:
                    tab.setText(R.string.tab_map);
                    break;
                case game:
                    tab.setText(R.string.tab_game);
                    break;
            }
        });
        tabLayoutMediator.attach();

    }

    private static class MyFragmentAdapter extends FragmentStateAdapter {
        public MyFragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position){
                case book:
                    return bookItemFragment.newInstance();
                case news:
                    return webViewFragment.newInstance();
                case map:
                    return MapFragment.newInstance();
                default:
                    return GameFragment.newInstance();
            }

        }

        @Override
        public int getItemCount() {
            return 4;
        }
    }

}